import { location } from "../utils/getLocation"
import settings from "../utils/settings";
import { sendEssentialNotifications, waypoints } from "../utils/utils";

class heavypearls {

    profiles = {}
    show = {
        days : 0,
        profiles : []
    }

    gui = new Gui();

    constructor() {
        this.load();
        this.initGui();
        this.init();
    }

    initGui() {
        this.gui.registerKeyTyped((typed,key) => {
            if(key == Keyboard.KEY_E) this.gui.close();
        })

        this.gui.registerDraw((mx,my,tick) => {

        })
    }

    load() {
        if(FileLib.exists("./config/ChatTriggers/modules/BabyzombieAddons/config/heavypearls.json")) 
            this.profiles = JSON.parse(FileLib.read("./config/ChatTriggers/modules/BabyzombieAddons/config/heavypearls.json"));
    }
    save() {
        FileLib.write("./config/ChatTriggers/modules/BabyzombieAddons/config/heavypearls.json",JSON.stringify(this.profiles,null,4),true);
    }

    showRemind() {
        Client.showTitle("&6&l摘珍珠啦~","&6[bza] &a提醒您该去摘今天的珍珠了",0,70,30);
        Client.showTitle("&6&l摘珍珠啦~","&6[bza] &a提醒您该去摘今天的珍珠了",0,70,30);
        new Message("&6[bza] &a该去摘今天的珍珠了 ", new TextComponent("&b[点击前往]").setClick("run_command","/warp crimson")).chat();
        //this.gui.open(); gui未完成
        sendEssentialNotifications("&6[bza] &a提醒您该去摘今天的珍珠了","点击此处前往地狱出生点",10,
            () => { 
                ChatLib.command("warp crimson"); 
                setTimeout(() => {
                    waypoints.addWaypoint("&bHeavy Pearls",-541,35,-889,0);
                }, 3000);
            },
            () => { Client.showTitle("","&a记得摘珍珠~",0,30,10); }
        );
    }

    init() {
        //检测 摘了没
        register("step", () => {
            if(!location.inSkyblock || location.island != "Crimson Isle" || location.location != "Matriarch's Lair") return;
            let date = new Date(Date.now());
            let days = date.getUTCFullYear() * 10000 + ( date.getUTCMonth() + 1 ) * 100 + date.getUTCDate();
            if(this.profiles[location.SBProfile] && this.profiles[location.SBProfile] == days) return;
            
            if(World.getAllEntitiesOfType(net.minecraft.entity.item.EntityArmorStand)
             .filter(e => e.distanceTo(-540,42,-888) < 4 && e.getName() === "§bHeavy Pearls Available: 0").length >= 1)
            {
                this.profiles[location.SBProfile] = days;
                this.save();
            }
        })

        //显示 该摘了
        register("worldLoad", () => {
            let date = new Date(Date.now());
            let days = date.getUTCFullYear() * 10000 + ( date.getUTCMonth() + 1 ) * 100 + date.getUTCDate();
            if(this.show.days != days) {
                this.show.days = days;
                this.show.profiles = [];
            }

            if(!settings.heavypearls || !location.inSkyblock) return;
            setTimeout(() => {
                if((this.profiles[location.SBProfile] == undefined || this.profiles[location.SBProfile] < days) &&
                 this.show.profiles.indexOf(location.SBProfile) == -1) {
                    this.show.profiles.push(location.SBProfile);
                    this.showRemind();
                }
            }, 2000);
        })
    }
}

export default new heavypearls();